package com.tianpengtech.web.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.tianpengtech.im.common.Const;
import com.tianpengtech.web.partybulid.model.CUser;
import com.tianpengtech.web.partybulid.model.CUserExt;
import com.tianpengtech.web.partybulid.model.DjCOrganize;
import com.tianpengtech.web.partybulid.model.ImMember;
import com.tianpengtech.web.partybulid.model.ImRoom;

public class MRoomController extends Controller {
	
	
	
	public void index() {
		//TODO 可增加token信息进行加密
		int uid = getParaToInt();
		CUserExt _us = CUserExt.dao.findById(uid);
		CUser us = CUser.dao.findById(uid);
		if(_us!=null){
			setAttr("userid", _us.getUserId());
			setAttr("organizid", us.getOrganizeId());
			setAttr("nickname", _us.getRealname());
			List<ImRoom> mRooms = ImRoom.dao.find("select * from im_room t WHERE t.isend='0' and  t.id in (select mid from im_member where uid = ?) ", _us.getUserId());
			setAttr("list", mRooms);
			setAttr("ip", Const.IP);
			setAttr("port", Const.PORT);
			
			/**用户列表**/
			List<DjCOrganize> oChilds = DjCOrganize.dao.queryChilds(us.getOrganizeId());
			List<DjCOrganize> oBrothers = DjCOrganize.dao.queryBrothers(us.getOrganizeId());
			/**去重复合并**/
			oChilds.removeAll(oBrothers);
			oChilds.addAll(oBrothers);
			
			List<CUserExt> cuChilds = CUserExt.dao.queryChilds(us.getOrganizeId());
			setAttr("members", cuChilds);
			
			render("index2.html");
		}else{
			renderError(500);
		}
		
	
	};
	public void addRoom(){
		int oid = getParaToInt("oid",0);
		/**用户列表**/
		List<DjCOrganize> oChilds = DjCOrganize.dao.queryChilds(oid);
		List<DjCOrganize> oBrothers = DjCOrganize.dao.queryBrothers(oid);
		
		/**去重复合并**/
		oChilds.removeAll(oBrothers);
		oChilds.addAll(oBrothers);
		
		renderJson(Ret.ok().set("list", oChilds));
		
	}
	
	@Before(Tx.class)
	public void saveRoom(){
	
		boolean flag1 = true,flag2 = true;
		Ret ret = new Ret();
		/**更新会议**/
		ImRoom room = getBean(ImRoom.class,"",true);
		room.setStime(new Date());
		Integer[] ids = getParaValuesToInt("ids");
		/**将创建者添加进成员**/
		List<Integer> _idlist = new ArrayList<>(Arrays.asList(ids));
		_idlist.add(room.getCid());
		ids = (Integer[])_idlist.toArray(new Integer[_idlist.size()]);
		/**创建会议**/
		flag1 = room.save();
		/**创建成员**/
		for(Integer id : ids){
		
			ImMember imMember = new ImMember();
			imMember.setMid(room.getId());
			imMember.setUid(id);
			if(!imMember.save()){
				flag2 = false;
			}
		}
		
		if(flag1 && flag2){
			ret = Ret.ok().set("obj",room).set("msg", Const.SUCCESS);
		}else{
			ret = Ret.fail().set("obj",room).set("msg", Const.ERR);;
		}
		renderJson(ret);
		
	}
	@Before(Tx.class)
	public void endRoom(){
		Ret ret = new Ret();
		int rid = getParaToInt("rid",0);
		ImRoom room = ImRoom.dao.findById(rid);
		room.setUsetime((new Date().getTime() - room.getStime().getTime())+"");
		room.setIsend("1");
		boolean flag = room.update();
		
		if(flag){
			ret = Ret.ok().set("obj",room).set("msg", Const.SUCCESS);
		}else{
			ret = Ret.fail().set("obj",room).set("msg", Const.ERR);;
		}
		
		
		renderJson(ret);
	}
	
}
